package com.century.sjt.window;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.activity.wallet.AddBankActivity;
import com.century.sjt.adapter.ChoosePayAdapter;
import com.century.sjt.entity.BankCard;
import com.century.sjt.entity.Merchant;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择支付方式
 */
public class PayPopupWindow extends PopupWindow{
    private Context mContext;
    ChoosePayPopupWindow menuWindow;
    private Button  btn_ok;
    private View mMenuView;
    private RequestQueue mQueue;
    //翻译监听
    private PayWindowListItemClickListener  listener;
    private BankCard payBankCard;
    private List<BankCard> mCardList = new ArrayList<>();
    private List<BankCard> mMTList = new ArrayList<>();
    private LinearLayout layout_Balance,layout_Quick_pay,layout_paypal_pay,layout_Addbankpay,layout_Card_pay;
    private TextView tv_Balance;
    private String amount;
    private String Type;
    private String Balance;
    private ListView lv_listview;

    public CheckBox checkBox;
    public Merchant Shop;
    private View view_1,view_2;
    private TextView icon_select2;
    private String toType;//支付入口
    private String orderId;

    //海外支付字段
    private String currencyCode;
    private String payMethod;
    private String currency;
    private ChoosePayAdapter adapter;
    private Handler mHandler;
    private TextView tv_phome_pay;
    public Context getmContext() {
        return mContext;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getPayMethod() {
        return payMethod;
    }

    public void setPayMethod(String payMethod) {
        this.payMethod = payMethod;
        if (!"6".equals(payMethod)){
            layout_paypal_pay.setEnabled(false);
            tv_phome_pay.setTextColor(mContext.getResources().getColor(R.color.textdefault_grey));
            layout_paypal_pay.setBackgroundColor(Color.rgb(220, 220, 220));
        }
    }

    public String getCurrencyCode() {
        return currencyCode;
    }

    public void setCurrencyCode(String currencyCode) {
        this.currencyCode = currencyCode;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public List<BankCard> getmCardList() {
        return mCardList;
    }

    public void setmCardList(List<BankCard> mCardList) {
        this.mCardList = mCardList;
        adapter.notifyDataSetChanged();
    }

    public List<BankCard> getmMTList() {
        return mMTList;
    }

    public void setmMTList(List<BankCard> mMTList) {
        this.mMTList = mMTList;
    }

    public String getBalance() {
        return Balance;
    }

    public void setBalance(String balance) {
        Balance = balance;
        tv_Balance.setText(balance);
    }

    public Merchant getShop() {
        return Shop;
    }

    public void setShop(Merchant shop) {
        Shop = shop;
    }

    public PayPopupWindow(final Activity context, final String Type, final String toType ,final List<BankCard> cardList ) {
        super(context);
        this.mContext = context;
        this.Type = Type;
        this.toType = toType;//1：商家 2：订单 3：充值
        this.mCardList = cardList;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.sjt_pay_window_item, null);
        mQueue = Volley.newRequestQueue(mMenuView.getContext());
        btn_ok = (Button) mMenuView.findViewById(R.id.btn_payok);
        checkBox = (CheckBox) mMenuView.findViewById(R.id.item_check);
        checkBox.setChecked(true);
        tv_phome_pay = (TextView) mMenuView.findViewById(R.id.tv_phome_pay);
        layout_Balance = (LinearLayout) mMenuView.findViewById(R.id.layout_pay1);
//        layout_Quick_pay = (LinearLayout) mMenuView.findViewById(R.id.layout_pay2);
//        layout_phone_pay = (LinearLayout) mMenuView.findViewById(R.id.layout_pay3);
        layout_paypal_pay = (LinearLayout) mMenuView.findViewById(R.id.layout_pay3);
//        layout_Card_pay = (LinearLayout) mMenuView.findViewById(R.id.layout_pay4);
        layout_Addbankpay = (LinearLayout) mMenuView.findViewById(R.id.layout_addbankpay);
        view_1 = (View) mMenuView.findViewById(R.id.view_1);

        icon_select2 = (TextView) mMenuView.findViewById(R.id.icon_select2);
        Typeface iconfont = Typeface.createFromAsset(mContext.getAssets(), "iconfont.ttf");
        icon_select2.setTypeface(iconfont);
        lv_listview = (ListView) mMenuView.findViewById(R.id.lv_listview);
        //设置按钮监听

        //设置PayPopupWindow的View
        this.setContentView(mMenuView);
        //设置PayPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置PayPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PayPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置PayPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置PayPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        if ("1".equals(Type)){
            layout_Balance.setVisibility(View.GONE);
            layout_Card_pay.setVisibility(View.GONE);
            view_1.setVisibility(View.GONE);
        }
        if ("3".equals(toType)){
            layout_paypal_pay.setVisibility(View.GONE);
        }
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });


        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        };

        adapter = new ChoosePayAdapter(mContext, mCardList, mHandler,"0");//0置灰，1显示信用卡
        lv_listview.setAdapter(adapter);
        lv_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if ("1".equals(mCardList.get(i).getAvailable())){
                    String name = mCardList.get(i).getCardName();
//                    listener.onItemClick(mCardList.get(i));
//                    listener.onPayTypeClick(mCardList);
                    listener.onPayTypeClick("1");
                    listener.onItemClick(mCardList.get(i));
                    dismiss();
                }else if ("1".equals(mCardList.get(i).getIsActive())){
                    final String phone = mCardList.get(i).getPhone();
                    final String bankCardNo = mCardList.get(i).getBankCardNo();
                    final String bankCode = mCardList.get(i).getBankCode();
                    final String bankName = mCardList.get(i).getCardName();
                    final String bankType = mCardList.get(i).getCardTypeName();
//                    final String status =  mlist.get(i).getStatus();
                    final AlertDialog alertDialog = new AlertDialog.Builder(context)
                            .setCancelable(true)
                            .show();
                    Window window = alertDialog.getWindow();
                    window.setContentView(R.layout.sjt_getmonilecode_alertdialog);
                    TextView tv_message = (TextView) window.findViewById(R.id.tv_dialog_message);
                    tv_message.setText(mContext.getResources().getString(R.string.text_activate_pay)) ;
                    Button btn_no = (Button) window.findViewById(R.id.btn_dialog_no);
                    btn_no.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            alertDialog.dismiss();
                        }
                    });
                    Button btn_ye = (Button) window.findViewById(R.id.btn_dialog_ye);
                    btn_ye.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            alertDialog.dismiss();
//                            listener.onDismissClick();
                            dismiss();
                            Intent intent = new Intent();
                            intent.setClass(context, AddBankActivity.class);
                            Bundle bundle = new Bundle();
                            if ("1".equals(toType)){
                                bundle.putSerializable("Merchant",Shop);
                                intent.putExtra("type","4");

                            }else if ("2".equals(toType)){
                                intent.putExtra("type","8");
                                intent.putExtra("orderId",orderId);
                            }else if ("3".equals(toType)){
                                intent.putExtra("type","1");
                            }

                            bundle.putString("phone",phone);
                            bundle.putString("bankCardNo",bankCardNo);
                            bundle.putString("bankCode", bankCode);
                            bundle.putString("bankName", bankName);
                            bundle.putString("bankType",bankType);
                            bundle.putString("status","0");
                            intent.putExtras(bundle);

                            context.startActivity(intent);

                        }
                    });
                }

            }
        });
//        menuWindow = new ChoosePayPopupWindow(context,getmCardList(),"0");
        layout_Balance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (checkBox.isChecked()){
//                    checkBox.setChecked(false);
//                }else {
//                    checkBox.setChecked(true);
//                }
                listener.onPayTypeClick("0");
                payBankCard = new BankCard();
                payBankCard.setCardName(mContext.getResources().getString(R.string.text_blance_pay));
                payBankCard.setBankCardNo(getBalance());
                listener.onItemClick(payBankCard);



                dismiss();
            }
        });
        layout_paypal_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onPayTypeClick("6");
                payBankCard = new BankCard();
                payBankCard.setCardName(mContext.getResources().getString(R.string.text_phone_pay));
                listener.onItemClick(payBankCard);

                dismiss();
            }
        });
//        layout_Quick_pay.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ShowChoseWindow(context,getmCardList(),"1",getShop(),getOrderId(),toType);
////                listener.onPayTypeClick("1");
//            }
//        });
//        layout_Phone_pay.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ShowChoseWindow(context,getmMTList(),"2",getShop(),getOrderId(),toType);
////                listener.onPayTypeClick("2");
//            }
//        });
//        layout_Card_pay.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                listener.onPayTypeClick("3");
//                payBankCard = new BankCard();
//                payBankCard.setCardName(mContext.getResources().getString(R.string.text_bankcard_pay));
//                payBankCard.setBankCardNo(getBalance());
//                listener.onItemClick(payBankCard);
//                dismiss();
//            }
//        });
        layout_Addbankpay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                Intent intent = new Intent();
                intent.setClass(mContext, AddBankActivity.class);
                mContext.startActivity(intent);

            }
        });
        tv_Balance = (TextView) mMenuView.findViewById(R.id.tv_balance);
        //确认按钮
        btn_ok.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });

    }
    public void ShowChoseWindow(Activity context, List<BankCard> mlist, String type, Merchant Shop,String OrderId, String toType){
        if (mlist.size()==0){
            showAppayRes();
        }else {
            menuWindow = new ChoosePayPopupWindow(context,mlist,type,Shop,OrderId,toType);
//        menuWindow.notifyDataSetChanged();
            menuWindow.showAtLocation(context.findViewById(R.id.paymacth), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
            menuWindow.setChoosePayWindowListItemClickListener(new ChoosePayPopupWindow.PayChooseWindowListItemClickListener() {
                @Override
                public void onItemClick(BankCard message) {
                    listener.onItemClick(message);
                    dismiss();
                }

                @Override
                public void onPayTypeClick(String Type) {
                    listener.onPayTypeClick(Type);
                }

                @Override
                public void onDismissClick() {
                    dismiss();
                }
            });
        }

    }

    public void showAppayRes() {
        //询问是否取消
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(mContext.getResources().getString(R.string.text_prompt));
        builder.setMessage(mContext.getResources().getString(R.string.error_noting_binding_bankcard));
        builder.setPositiveButton(mContext.getResources().getString(R.string.btn_yes), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                dismiss();
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                if ("1".equals(toType)){
                    bundle.putSerializable("Merchant", getShop());
                    intent.putExtra("type","4");

                }else if ("2".equals(toType)){
                    intent.putExtra("type","8");
                    intent.putExtra("orderId",getOrderId());
                }else if ("3".equals(toType)){
                    intent.putExtra("type","1");
                }
                intent.putExtras(bundle);
                intent.setClass(mContext, AddBankActivity.class);
                mContext.startActivity(intent);
            }
        });
        builder.setNegativeButton(mContext.getResources().getString(R.string.btn_no), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setCancelable(false);
        builder.create().show();
    }
    public void setPayWindowListItemClickListener(PayWindowListItemClickListener listener) {
        this.listener = listener;
    }
    public interface PayWindowListItemClickListener{
        /**
         * 支付方式事件监听
         * @param message
         * @return
         */
        void onItemClick(BankCard message);

        void onPayTypeClick(String Type);

    }
}
